package crm.model.teacherPC.matnr;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.TreeMap;

import crm.model.Setting;
import crm.util.ErrorHandler;

public class MatNr {

	public static TreeMap<String, Schueler> map;


	static {
		map = new TreeMap<String, Schueler>();
		System.out.println("hello");
		File f = Setting.getMathNrFile();
		if (f.exists()){
			parsCSV(f);
		}else {
			ErrorHandler.showAndGoOnErrorMessage("MatNr.csv wurde nicht gefunden");
		}

	}


	private static void parsCSV(File f) {
		try {
			FileReader reader = new FileReader(f);
			BufferedReader bReader = new BufferedReader(reader);
			bReader.skip(56);
			int chI = 0;
			StringBuilder build = new StringBuilder();
			int pos = 0;
			Schueler schueler = new Schueler();
			while((chI = bReader.read()) != -1){
				char ch = (char)chI;

				if (ch == ';'){
					if (pos == 0){
						schueler.artikelNr = build.toString();
						System.out.println("artikelnr: " + build.toString());
					}else if (pos == 1){
						schueler.gruppe = build.toString();
					}else if (pos == 2){
						schueler.Klasse = build.toString();
					}else if (pos == 3){
						schueler.id = build.toString();
					}else if (pos == 4){
						schueler.nName = build.toString();
					}


					build = new StringBuilder();
					pos++;
				}else if (ch == '\n'){
					schueler.vName = build.toString();
					pos = 0;
					build = new StringBuilder();
					map.put(schueler.artikelNr, schueler);
					schueler = new Schueler();
				}else {
					if (ch == '\r'){

					}else {
						build.append(ch);
					}
				}

			}

		}catch(IOException e){
			e.printStackTrace();
		}
//		String str = map.toString();
//		str = str.replace("],", "],\n");
//		System.out.println(str);
//		System.out.println(map.size());
	}


}

